//
//  StatusListCollectionViewCell.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/27.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "StatusListCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "NSString+Font.h"

@implementation StatusListCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 头像
        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
        _iconImageView.layer.cornerRadius = 25;
        _iconImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_iconImageView];
        // 用户昵称
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 10, 0, 30)];
        _userNameLabel.font = [UIFont systemFontOfSize:15.0];
        [self.contentView addSubview:_userNameLabel];
        // 创建时间
        _createTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 40, 0, 20)];
        _createTimeLabel.font = [UIFont systemFontOfSize:13.0];
        _createTimeLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_createTimeLabel];
        // 来源
        _sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 40, 0, 20)];
        _sourceLabel.font = [UIFont systemFontOfSize:13.0];
        _sourceLabel.textColor = [UIColor grayColor];
        
        [self.contentView addSubview:_sourceLabel];
        
        // 内容
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, kWidth - 30, 0)];
        _contentLabel.font = [UIFont systemFontOfSize:15.0];
        _contentLabel.numberOfLines = 0;
        [self.contentView addSubview:_contentLabel];
        
        
    }
    return self;
}


- (void)setStatus:(Status *)status {
    _status = status;
    // 头像
    [_iconImageView sd_setImageWithURL:[NSURL URLWithString:status.user.avatar_hd]];
    
    [self setNameLabelTextWithString:status.user.name];
    [self setCreateTimeLabelTextWithString:status.created_at];
    [self setSourceLabelTextWithString:status.source];
    [self setContentLabelTextWithString:status.text];
}

// 设置昵称
- (void)setNameLabelTextWithString:(NSString *)string {
    CGRect newFrame = _userNameLabel.frame;
    newFrame.size.width = [string getTextWidthWithFont:[UIFont systemFontOfSize:15] height:30];
    _userNameLabel.frame = newFrame;
    _userNameLabel.text = string;
}

// 设置创建时间
- (void)setCreateTimeLabelTextWithString:(NSString *)string {
    NSString *dateString = [self resolveSinaWeiboDate:string];
    
    CGRect newFrame = _createTimeLabel.frame;
    newFrame.size.width = [dateString getTextWidthWithFont:[UIFont systemFontOfSize:13] height:20];
    _createTimeLabel.frame = newFrame;
    _createTimeLabel.text = dateString;
    
}

// 设置来源信息
- (void)setSourceLabelTextWithString:(NSString *)string {
    _sourceLabel.frame = CGRectMake(70, 40, 0, 20);
    
     // 1.创建正则表达式，[0-9]:表示‘0’到‘9’的字符的集合
    NSString *pattern = @"<a\\s+[^>]*?>";
   
    // 1.1将正则表达式设置为OC规则
    NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    // 2.利用规则测试字符串获取匹配结果
    NSArray *results = [regular matchesInString:string options:0 range:NSMakeRange(0, string.length)];
   
    if (results.count == 0) {
        return;
    }
    NSTextCheckingResult *result = results.firstObject;
    NSString *resultString = [string substringWithRange:result.range];
    
    
    NSRange range1 = [string rangeOfString:resultString];
    NSRange range2 = [string rangeOfString:@"</a>"];
    
    NSString *s = [string substringWithRange:NSMakeRange(range1.length + range1.location, range2.location - range1.location - range1.length)];
    NSString *source = [NSString stringWithFormat:@"来自%@",s];
    CGRect newFrame = _sourceLabel.frame;
    
    newFrame.size.width = [source getTextWidthWithFont:[UIFont systemFontOfSize:13] height:30];
    newFrame.origin.x += _createTimeLabel.frame.size.width;
    _sourceLabel.frame = newFrame;
    _sourceLabel.text = source;
}

- (void)setContentLabelTextWithString:(NSString *)string {
//    _contentLabel.frame = CGRectMake(10, 70, kWidth - 20, 30);
   
    CGRect newFrame = _contentLabel.frame;
    newFrame.size.height = [string getTextHeightWithFont:[UIFont systemFontOfSize:15] width:kWidth - 30];
    NSLog(@"%f", newFrame.size.height);
    _contentLabel.frame = newFrame;
    _contentLabel.text = string;
    

}

//解析新浪微博中的日期

- (NSString*)resolveSinaWeiboDate:(NSString*)date{
    NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];
    iosDateFormater.dateFormat=@"EEE MMM d HH:mm:ss Z yyyy";
    
    iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    NSDate *inputDate=[iosDateFormater dateFromString:date];
    
    //目的格式
    NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
    //[resultFormatter setDateFormat:@"YY-MM-dd HH:mm"];
    [resultFormatter setDateFormat:@"HH:mm"];
    return [resultFormatter stringFromDate:inputDate];
}


@end
